//
//  LMPostsController.m
//  EPA
//
//  Created by lanou3g on 16/4/22.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMPostsController.h"
#import "LMDetailController.h"

#import "LMNetWorkRequestManager.h"

#import "LMPostsModel.h"

#import "LMPostsCell.h"
#import "LMTextModelCell.h"
#import "LMImageModelCell.h"

@interface LMPostsController ()

@property (nonatomic, strong) NSMutableArray *postsArray;

@property (nonatomic, strong) NSString *start;

@end

@implementation LMPostsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.postsArray = [NSMutableArray array];
    self.start = @"0";
    [self loadDataFormServerStart:self.start];
    UIView *view  = [[UIView alloc]initWithFrame:self.tableView.frame];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"beijing.JPG"]];
    [self.tableView setBackgroundView:view];
    
    [self.tableView registerNib: [UINib nibWithNibName:@"LMTextModelCell" bundle:nil] forCellReuseIdentifier:@"LMTextModel"];
      [self.tableView registerNib: [UINib nibWithNibName:@"LMImageModelCell" bundle:nil] forCellReuseIdentifier:@"LMImageModel"];
    [self addRefresh];
}

#pragma mark - 解析数据
- (void)loadDataFormServerStart :(NSString *)start{
    NSDictionary *params = [NSDictionary dictionary];
    if ([UserInfoManager shareManager].auth) {
        params =@{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"client":@"1",@"sort":@"addtime",@"limit":@(10),@"version":@"3.0.2",@"start":start,@"auth":[UserInfoManager shareManager].auth};
    }else{
        params = @{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"client":@"1",@"sort":@"addtime",@"limit":@(10),@"version":@"3.0.2",@"start":start,@"auth":@""};
    }
    [LMNetWorkRequestManager requestURL:[NSURL URLWithString:TOPICLIST_URL] Method:LM_POST Params:params
 Result:^(NSData *data, NSURLResponse *response, NSError *error) {
     NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
     NSDictionary *dataDict = jsonDict[@"data"];
     for (NSDictionary *item in dataDict[@"list"]) {
         LMPostsModel *model = [LMPostsModel modelWithDictionary:item];
         [self.postsArray addObject:model];
         NSLog(@"%@",model);
     }
     dispatch_async(dispatch_get_main_queue(), ^{
         [self.tableView reloadData];
         [[self.tableView defaultHeader] endRefreshing];
         [[self.tableView defaultFooter] endRefreshing];
     });
 }];
    
    
}

#pragma mark - 上下刷新机制
- (void)addRefresh{
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [weakSelf.postsArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataFormServerStart:@"0"];
    }];
    
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        weakSelf.start = [NSString stringWithFormat:@"%ld",weakSelf.postsArray.count];
        [weakSelf loadDataFormServerStart:weakSelf.start];
    }];
    
}

-(void)dealloc{
    [self.tableView removeObserver:[self.tableView defaultHeader] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.postsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [LMPostsCell cellWithTableView:tableView Model:self.postsArray[indexPath.row] indexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *mdelName = NSStringFromClass([self.postsArray[indexPath.row] class]);
    Class CellClass = NSClassFromString([mdelName stringByAppendingString:@"Cell"]);
    return [CellClass height];
   
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LMDetailController *lmVC = [[LMDetailController alloc]init];
    LMPostsModel *model =self.postsArray[indexPath.row];
    lmVC.contentId = model.contentid;
    lmVC.title = model.title;
    [self.navigationController pushViewController:lmVC animated:YES];
}


@end
